<script setup lang="ts">
// 获取我的用户信息
// 1：定义api  2:导入api  3:进入页面调用api  4:定义返回值的数据类型  5：存储数据并渲染
import { patientMyUser } from '@/api/user'
import { ref } from 'vue'
import { onMounted } from 'vue'
import type { patientMyUserApi } from '@/types/user'
import { showConfirmDialog, showSuccessToast } from 'vant'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute() // route===this.$route
const store = useUserStore()
const info = ref<patientMyUserApi>()
const getData = async () => {
  const res = await patientMyUser()
  info.value = res.data
  console.log('用户信息', res)
}
onMounted(() => {
  getData()
})

// 退出登录
const logout = async () => {
  // 确定框
  // 清除用户信息与token
  // 提示
  // 跳转到登录页
  await showConfirmDialog({
    title: '温馨提示',
    message: '您确定退出该网站吗？'
  })
  store.logout()
  showSuccessToast('退出登录成功')
  // 传：带上当前页面的路由
  // console.log(route)
  router.push('/login?redirect=' + encodeURIComponent(route.fullPath))
}
</script>

<template>
  <div class="user-page">
    <div class="user-page-head">
      <div class="top">
        <van-image round fit="cover" :src="info?.avatar" />
        <div class="name">
          <p>{{ info?.account }}</p>
          <p><van-icon name="edit" /></p>
        </div>
      </div>
      <van-row>
        <van-col span="6">
          <p>{{ info?.collectionNumber }}</p>
          <p>收藏</p>
        </van-col>
        <van-col span="6">
          <p>{{ info?.likeNumber }}</p>
          <p>关注</p>
        </van-col>
        <van-col span="6">
          <p>{{ info?.score }}</p>
          <p>积分</p>
        </van-col>
        <van-col span="6">
          <p>{{ info?.collectionNumber }}</p>
          <p>优惠券</p>
        </van-col>
      </van-row>
    </div>
    <div class="user-page-order">
      <div class="head">
        <h3>药品订单</h3>
        <router-link to="/order">全部订单 <van-icon name="arrow" /></router-link>
      </div>
      <van-row>
        <van-col span="6">
          <van-badge :content="info?.orderInfo.paidNumber || ''">
            <cp-icon name="user-paid" />
          </van-badge>
          <p>待付款</p>
        </van-col>
        <van-col span="6">
          <van-badge :content="info?.orderInfo.receivedNumber || ''">
            <cp-icon name="user-shipped" />
          </van-badge>
          <p>待发货</p>
        </van-col>
        <van-col span="6">
          <van-badge :content="info?.orderInfo.shippedNumber || ''">
            <cp-icon name="user-received" />
          </van-badge>
          <p>待收货</p>
        </van-col>
        <van-col span="6">
          <van-badge :content="info?.orderInfo.finishedNumber || ''">
            <cp-icon name="user-finished" />
          </van-badge>
          <p>已完成</p>
        </van-col>
      </van-row>
    </div>
    <!-- 快捷工具 -->
    <div class="tool">
      <div class="title">快捷工具</div>
      <van-cell title="我的问诊" is-link center to="/user/consult">
        <template #icon>
          <CpIcon name="user-tool-01"></CpIcon>
        </template>
      </van-cell>
      <van-cell title="我的处方" is-link center to="/">
        <template #icon>
          <CpIcon name="user-tool-02"></CpIcon>
        </template>
      </van-cell>
      <van-cell title="家庭档案" is-link center to="/user/patient">
        <template #icon>
          <CpIcon name="user-tool-03"></CpIcon>
        </template>
      </van-cell>
      <van-cell title="地址管理" is-link center to="/user/address">
        <template #icon>
          <CpIcon name="user-tool-04"></CpIcon>
        </template>
      </van-cell>
      <van-cell title="我的评价" is-link center to="/">
        <template #icon>
          <CpIcon name="user-tool-05"></CpIcon>
        </template>
      </van-cell>
      <van-cell title="官方客服" is-link center to="/">
        <template #icon>
          <CpIcon name="user-tool-06"></CpIcon>
        </template>
      </van-cell>
      <van-cell title="设置" is-link center to="/">
        <template #icon>
          <CpIcon name="user-tool-07"></CpIcon>
        </template>
      </van-cell>
    </div>
    <a class="logout" href="javascript:;" @click="logout">退出登录</a>
  </div>
</template>

<style lang="scss" scoped>
.user-page {
  background-color: var(--cp-bg);
  min-height: calc(100vh - 50px);
  padding: 0 15px 65px;
  // 头部
  &-head {
    height: 200px;
    background: linear-gradient(180deg, rgba(44, 181, 165, 0.46), rgba(44, 181, 165, 0));
    margin: 0 -15px;
    padding: 0 15px;
    .top {
      display: flex;
      padding-top: 50px;
      align-items: center;
      .van-image {
        width: 70px;
        height: 70px;
      }
      .name {
        padding-left: 10px;
        p {
          &:first-child {
            font-size: 18px;
            font-weight: 500;
          }
          &:last-child {
            margin-top: 10px;
            color: var(--cp-primary);
            font-size: 16px;
          }
        }
      }
    }
    .van-row {
      margin: 0 -15px;
      padding-top: 15px;
      p {
        text-align: center;
        &:first-child {
          font-size: 18px;
          font-weight: 500;
        }
        &:last-child {
          color: var(--cp-dark);
          font-size: 12px;
          padding-top: 4px;
        }
      }
    }
  }
  // 订单
  &-order {
    background-color: #fff;
    border-radius: 8px;
    margin-bottom: 15px;
    padding-bottom: 15px;
    .head {
      display: flex;
      justify-content: space-between;
      line-height: 50px;
      padding: 0 15px;
      a {
        color: var(--cp-tip);
      }
    }
    .van-col {
      text-align: center;
      .cp-icon {
        font-size: 28px;
      }
      p {
        font-size: 12px;
        padding-top: 4px;
      }
    }
  }
  // 分组
  &-group {
    background-color: #fff;
    border-radius: 8px;
    overflow: hidden;
    h3 {
      padding-left: 16px;
      line-height: 44px;
    }
    .van-cell {
      align-items: center;
    }
    .cp-icon {
      font-size: 17px;
      margin-right: 10px;
    }
  }
  .logout {
    display: block;
    margin: 20px auto;
    width: 100px;
    text-align: center;
    color: var(--cp-price);
  }
}
.tool {
  background-color: #fff;
  margin-top: 15px;
}
.title {
  font-size: 16px;
  font-weight: 600;
  margin-left: 15px;
  padding-top: 10px;
}
:deep() {
  .van-cell__title {
    margin-left: 10px;
  }
}
</style>
